package com.health.auth.controller;

import com.health.auth.form.PatientLoginBody;
import com.health.auth.service.PatientLoginService;
import com.health.common.core.domain.R;
import com.health.common.core.utils.JwtUtils;
import com.health.common.core.utils.StringUtils;
import com.health.common.security.auth.AuthUtil;
import com.health.common.security.service.TokenService;
import com.health.common.security.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @BelongsProject: dimensionality-manage
 * @BelongsPackage: com.health.auth.controller
 * @Author: 许力匀
 * @CreateTime: 2024-12-13  22:25
 * @Description: 患者端登录
 * @Version: 1.0
 */
@RestController
@RequestMapping("Patient")
public class PatientController {

    @Autowired
    private PatientLoginService patientLoginService;

    /*
    等出
     */
    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
        }
        return R.ok();
    }

    /*
    患者登录
     */
    @PostMapping("login")
    public R<?> loginEmail(@RequestBody PatientLoginBody patientLoginBody)
    {
        return R.ok(patientLoginService.loginEmail
            (patientLoginBody.getEmail(), patientLoginBody.getPassword())) ;
    }



}
